package com.lxm.controller;


import com.lxm.dto.ItemDTO;
import com.lxm.dto.Result;
import com.lxm.service.ItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/user/item")
@Tag(name = "商品管理")
public class ItemController {
    @Resource
    private ItemService itemService;

    /**
     * 添加商品
     *
     * @param itemInfo ItemDTO
     * @return Result
     */

    @Operation(summary = "添加商品")
    @PostMapping
    //图片URL由前端上传
    public Result addItem(@RequestBody ItemDTO itemInfo) {
        return itemService.addItem(itemInfo);
    }

    @Operation(summary = "修改商品信息")
    @PutMapping("/{itemId}")
    public Result modifyItem(@RequestBody ItemDTO itemInfo, @PathVariable Long itemId) {
        return itemService.modifyItem(itemInfo, itemId);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/delete/item")
    //前端需要判断是否有设置删除id，不然不发请求
    public Result removeItem(@RequestBody List<Long> itemIds) {
        return itemService.removeItems(itemIds);
    }

    @Operation(summary = "获取商品类型")
    @GetMapping("/type")
    public Result getItemTypeList() {
        return itemService.getItemTypeList();
    }

    @Operation(summary = "获取按类型分类时间排序的列表", description = "用于商品分类页")
    @GetMapping("/listByType")
    public Result getItemListByType(@RequestParam(name = "typeId") @NotNull Long typeId
            , @RequestParam(name = "current", defaultValue = "1") Integer current) {
        return itemService.getItemListByType(typeId, current);
    }

    @Operation(summary = "分页获取商品列表")
    @GetMapping("/listByTime/firstPage")
    public Result getItemList(@RequestParam(name = "current", defaultValue = "1")
                              Integer current) {
        return itemService.listItemByTime(current);
    }

    @Operation(summary = "获取自己发布的商品详情信息", description = "获取自己发布的商品分页列表列表")
    @GetMapping("/myItemList")
    public Result getMyItemList(@RequestParam(name = "current", defaultValue = "1")
                                Integer current) {
        return itemService.getMyItemList(current);
    }

    @Operation(summary = "根据商品id获取商品详情", description = "这是初次获取商品详细信息，所以里面的评论分页默认为1")
    @GetMapping("/detail/{id}")
    public Result getItemDetail(@PathVariable("id") Long itemId) {
        return itemService.getItemDetail(itemId);
    }
}

